package com.todo.strategy.online.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.strategy.online.infrastructure.entity.po.PlanContentPO;
import com.todo.strategy.online.infrastructure.mapper.PlanContentMapper;
import com.todo.strategy.online.infrastructure.repository.PlanContentRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (PlanContent)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("planContentRepository")
public class PlanContentRepositoryImpl implements PlanContentRepository {
    @Resource
    private PlanContentMapper planContentMapper;


    @Override
    public Boolean insertPlanContentPO(PlanContentPO planContentPO) {
        if (planContentPO == null) {
            return false;
        }
        if (!baseFieldCheck(planContentPO)) {
            return false;
        }
        return planContentMapper.insertPlanContentPO(planContentPO) > 0;
    }

    @Override
    public Boolean updatePlanContentPOById(PlanContentPO planContentPO) {
        if (planContentPO == null || planContentPO.getId() == null) {
            return false;
        }
        if (!baseFieldCheck(planContentPO)) {
            return false;
        }
        return planContentMapper.updatePlanContentPOById(planContentPO) > 0;
    }


    @Override
    public Boolean removePlanContentPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return planContentMapper.removePlanContentPOById(id) > 0;
    }

    @Override
    public PlanContentPO queryPlanContentPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return planContentMapper.queryPlanContentPOById(id);
    }

    @Override
    public PlanContentPO queryPlanContentPOByPlanRelationId(Long userId, Long planRelationId) {
        if (planRelationId == null || planRelationId < 0) {
            return null;
        }
        return planContentMapper.queryPlanContentPOByPlanRelationId(userId, planRelationId);
    }

    @Override
    public List<PlanContentPO> queryPlanContentPOByPlanId(Long userId, Long planId) {
        if (planId == null || planId < 0) {
            return Lists.newArrayList();
        }
        return planContentMapper.queryPlanContentPOByPlanId(userId, planId);
    }

    @Override
    public Long removePlanContentPOByPlanId(Long userId, Long planId) {
        if (planId == null || planId < 0) {
            return null;
        }
        List<PlanContentPO> planContentPOS = queryPlanContentPOByPlanId(userId, planId);
        if (CollectionUtils.isEmpty(planContentPOS)) {
            return 0L;
        }
        List<Long> idList = planContentPOS.stream().map(PlanContentPO::getId).collect(Collectors.toList());
        return planContentMapper.removePlanContentPOByIdList(idList);
    }

    private boolean baseFieldCheck(PlanContentPO planContentPO) {
        if (planContentPO == null) {
            return false;
        }
        if (planContentPO.getPlanRelationId() == null || planContentPO.getPlanId() == null || planContentPO.getUserId() == null) {
            log.warn("PlanContentRepository#baseFieldCheck 核心字段校验失败，默认操作失败 planContentPO：【{}】", JSONObject.toJSONString(planContentPO));
            return false;
        }
        if (planContentPO.getTitle() == null || planContentPO.getContent() == null) {
            log.warn("PlanContentRepository#baseFieldCheck 核心字段校验失败，默认操作失败 planContentPO：【{}】", JSONObject.toJSONString(planContentPO));
            return false;
        }
        return true;
    }
}
